package com.leyou.user.web;

import com.leyou.common.exceptions.LyException;
import com.leyou.user.dto.AddressDTO;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @package com.leyou.user.web
 * @description: 用户模块的控制层
 * @author: 许超越
 * @date 2019/7/6 17:11
 * @version: V1.0
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 校验数据是否可用
     *
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUserData(@PathVariable("data") String data, @PathVariable("type") Integer type) {
        return ResponseEntity.ok(userService.checkUserData(data, type));
    }


    /**
     * 发送短信验证码
     *
     * @param phone 手机号码
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone) {
        userService.sendCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 注册用户
     *
     * @param user 用户信息
     * @param code 验证码
     * @return
     */
    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, BindingResult result, @RequestParam("code") String code) {
        //对前台用户传递的数据进行校验
        if (result.hasErrors()) {
            String message = result.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            throw new LyException(400, message);
        }
        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 根据用户名和密码查询用户
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping("query")
    public ResponseEntity<UserDTO> queryUserByUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password) {
        return ResponseEntity.ok(userService.queryUserByUsernameAndPassword(username, password));
    }


    /**
     * 查询当前登入用户的收件地址
     *
     * @return
     */
    @GetMapping("addressList")
    public ResponseEntity<List<AddressDTO>> queryAddress(HttpServletRequest request) {
        return ResponseEntity.ok(userService.queryAddress(request));
    }


    /**
     * 根据用户id和地址id查询查询地址
     * @param id
     * @param userId
     * @return
     */
    @GetMapping("address")
    public ResponseEntity<AddressDTO> queryAddressByUserIdAndId(@RequestParam("id") Long id, @RequestParam("userId") Long userId){
        return ResponseEntity.ok(userService.queryAddressByUserIdAndId(id, userId));
    }

}
